package com.example.sample;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetUrl {

	public String getStringFromFacebook(){
		StringBuilder sb = new StringBuilder();
		
		try{
			HttpClient client = new DefaultHttpClient();
	        HttpGet get = new HttpGet();
	        get.setURI(new URI("https://graph.facebook.com/me/albums?access_token=" + V.FACEBOOK_ACCESS_TOKEN ));
	        HttpResponse resp = client.execute(get);
	        BufferedReader br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
	        String str = null;
	        while((str =  br.readLine()) != null)
	        {
	        	sb.append(str).append("\n");
	        }br.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		 
		return sb.toString();
	}
	 
	 
	public String getJsonText(){
	
		 String link = null;
		 
		 try{
			 String line = getStringFromFacebook();
			 JSONObject object = new JSONObject(line);
			 JSONArray dataArray = new JSONArray(object.getString("data"));
			 for(int i=0;i<dataArray.length();i++){
				 String temp;
				 JSONObject dataObj = dataArray.getJSONObject(i);
				 temp = dataObj.getString("name");
				 if(temp.equals(C.FACEBOOK_ALBUM)){
					 link = dataObj.getString("link").replace("www", "m");
				 }
			 }
		 }catch (JSONException e) {
			 e.printStackTrace();
		 }
		 return link;
	 }
}
